<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ include file="../../../attr.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

<!doctype html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=emulateIE7" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta charset="utf-8">
<link rel="stylesheet" href="${ctx}/css/sapar.css" />
<link rel="stylesheet" type="text/css" href="${ctx}/css/common.css" />
<script type="text/javascript" src="${ctx}/js/jquery.js"></script>
<script type="text/javascript" src="${ctx}/js/sapar.js"></script>
<script type="text/javascript" src="${ctx}/js/WdatePicker.js"></script>
<script type="text/javascript" src="${ctx}/assets/layer/layer.js"></script>
<script type="text/javascript" src="${ctx}/js/kkpager.min.js"></script>
<link rel="stylesheet" type="text/css" href="${ctx}/css/kkpager_blue.css" />
<script type="text/javascript" src="${ctx}/js/mask.js"></script>
<script type="text/javascript" src="${ctx}/js/jquery-1.10.1.js"></script>

<title>申请信息</title>
<style  type="text/css">
	#left{
		float:left; width:100%;
	}
	#saper-container{float:left;width:100%;background: url(${ctx}/images/dian.png) repeat-y 69% 0;}
	#table_left{float:left; width:68%;min-height:300px}
	#right{
		float:right; width:30%;min-height:300px
	} 
	#fafangrentiaoma{
	height:25px;
	}
	.search-box .iselect-wrapper{
		float: left;
		margin-right: 5px;
		width: 150px;
	}
	#fangfar{
	    margin-left:100px;
	    float: left;
	    padding-top: 5px;
	}
	#fangfarentm{
	    margin-left:15px;
	    float: left;
	}
	.search-box .iselect-wrapper .iselect{
		width:150px;
	}
	.fafang{
	margin-left:10px;
	}
	#weifa{
	color:red;
	font-weight:bold;
	}
	#yifa{
	color:green;
	font-weight:bold;
	}
	#keshi{
	width:176px;
	}
	#queren{
	margin-left:150px;
	}
	th,td{
	text-align: center;
	}
</style>

</head>

<body>
<div id="mask" class="mask"></div>
<div id="left">
	<div id="saper-container">
        <div id="saper-hd"></div>
        <div id="saper-bd">
            <div class="subfiled clearfix">
                <h2>申请信息</h2>
            </div>
            <div class="subfiled-content">
                <div class="search-box clearfix">
                    <div class="kv-item clearfix">
                       
                        <label>选择科室：</label>
                        <div class="kv-item-content">
                            <select class="search_text" id="keshi">
                                	<option value="" >请选择</option>
                               				<c:if test="${alldeparts != null}">
			                            	<c:forEach items="${alldeparts}" var="pro">
			                            	<c:if test="${pro.departmentId!=15 }">
			                                	<option value="${pro.departmentName}">${pro.departmentName}</option>
			                                </c:if>
			                                </c:forEach>
			                                </c:if>
                            </select>
                        </div>
                    </div>
                    <a href="javascript:;" class="sapar-btn sapar-btn-recom query-btn">查询</a>
                        <div>
	                        <div id="fangfar">
			                    <label id="fangfaren">&nbsp;&nbsp;&nbsp;发放人：</label>
			                </div>
	                        <div class="kv-item-content" id="fangfarentm">
								<input type="text" name="" placeholder="发放人条码"
									 class="fafangren" id="fafangrentiaoma" onblur="ckeckfafangren()" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')">
							</div>
						</div>	
	                    <a href="javascript:;" class="sapar-btn sapar-btn-recom fafang" id="queren">发放</a>
                    </div>
                

                <!--表格开始-->
                <div id="table_left" class="table">
                    <!--表格具体内容-->
                    <div class="table-box">
						<table>
							<thead>
								<tr>
									<th><input type="checkbox" id="checkall" name="checkall"/></th>
									<th>申请流水</th>
									<th>申请科室</th>
									<th>申请人</th>
									<th>申请时间</th>
									<!-- <th>审核人条码</th>
									<th>审核时间</th> -->
									<!-- <th>审核状态</th> -->
									<th>申请分类</th>
									<th>发放状态</th>
									<th>记录时间</th>
								</tr>
							</thead>
							<tbody>
								<c:if test="${shenqingList != null}">
									<c:forEach items="${shenqingList}" var="shenqing">
										<tr  data="${shenqing.shenqingFenlei}" data2="${shenqing.shenqingLiushui}"  class="ycpdata">
											<td><input type="checkbox" name="key"
												value="${shenqing.shenqingLiushui}" shuju="${shenqing.shenqingFenlei}" zhuangtai="${shenqing.fafangZhuangtai}"/></td>
											<td>${shenqing.shenqingLiushui}</td>
											<td>${shenqing.department.departmentName}</td>
											<td>${shenqing.shenqingren.employeeName}</td>
											<td><fmt:formatDate value="${shenqing.shenqingTime}"
													type="date" pattern="yyyy-MM-dd HH:mm:ss" /></td>
											<%-- <td>
												<c:if test="${shenqing.shenqingShenheZhuangtai==1}">通过</c:if>
												<c:if test="${shenqing.shenqingShenheZhuangtai==0}">未通过</c:if>
											</td> --%>
											<td class="fenlei" value="${shenqing.shenqingFenlei}">
												<c:if test="${shenqing.shenqingFenlei==1}">一次品</c:if>
												<c:if test="${shenqing.shenqingFenlei==0}">通用包</c:if>
											</td>
											<td id="fafangzhuangtai">
												<c:if test="${shenqing.fafangZhuangtai==0}"><span id="weifa">未发</span></c:if>
												<c:if test="${shenqing.fafangZhuangtai==1}"><span id="yifa">已发</span></c:if>
											</td>
											<td><fmt:formatDate value="${shenqing.recordTime}"
													type="date" pattern="yyyy-MM-dd HH:mm:ss" /></td>
										</tr>
									</c:forEach>
								</c:if>
							</tbody>
						</table>
					</div>
					
		<div id="saper-ft">
			<div id="kkpager"></div>
		</div>
        </div><!--表格结束-->
        <div id="right">
<!--表格开始-->
    <div class="table">
	<div class="table-box">
						 <div class="table-operate ue-clear">
                    	 	<a href="javascript:;" class="view">查看</a> 
                    	 </div>
                        <table id="detail">
                            <thead>
                                <tr>
                                	<!-- <th><input type="checkbox" name="keytwo" value="" id="box"/></th> -->
                                    <th>包名称</th>
                                    <th>包数量</th>
                                    <th>发放状态</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                     <div>
						<input id="leftPage" class="butLeft" type="button" value="上一页" />
						<input id="rightPage" class="butRight" type="button" value="下一页" />
						<span>到第&nbsp;</span><input id="goPage" class="textBox" type="text" value="1" /><span style="margin-left: 4px;">页&nbsp;</span>
						<input  id="sureBut" class="sure" type="button" value="确定" />
						<span>共</span><span class="totalPages" id="totalNum">0</span><span>&nbsp;页</span>
					</div>
                    </div>
              </div>
            </div>
        </div>
    </div>
   </div> 
</body>
<script type="text/javascript">
var pageSize = 10 , currentPage = 1 , totalPage;
var data = [];
var num=0;
var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	$(function(){
		var totalPage = ${total};
		var pageNo = getParameter('pageIndex');
		if(!pageNo){
			pageNo = 1;
		}
		//生成分页
		//有些参数是可选的，比如lang，若不传有默认值
		kkpager.generPageHtml({
			pno : pageNo,
			//总页码
			total : totalPage,
			//链接前部
			hrefFormer : '${ctx}/shenqing/shenqingList',
			//链接尾部
			hrefLatter : '.do',
			getLink : function(n){
				return this.hrefFormer + this.hrefLatter + "?pageIndex="+n+"&searchTxt="+$(".search_text").val();
			}
		});
		
		$(".query-btn").click(function(){
			window.location.href="${ctx}/shenqing/shenqingList.do?searchTxt="+$(".search_text").val();
		});
		
		$("#checkall").click(function(){
	   		   if(this.checked){
	   		        $("input[name='key']").prop('checked', true)
	   		    }else{
	   		        $("input[name='key']").prop('checked', false)
	   		    }

		});
		<%--发放的页面事件--%>
		$(".fafang").click(function(){
			//var shuliang=$("#zongshu").val();
			var btn=true;
			var ffzhuangtai=getfafangzhuangtai();
			var liushui=getshenqingliushui();
			var chk_value = getSelRecord();
			var mingxiliushui=getshuju();
			var leixin=getleixin();
			var fafangren=$("#fafangrentiaoma").val();
			var keshi=$("#keshi").val();
//			var mingxiliushui=getshuju();
			if(isNullOrEmpty(fafangren)){
				layer.msg("请输入发放人");
				btn=false;
				return btn;
			}
			if(ffzhuangtai==1){
				layer.msg("该物品已发放");
				btn=false;
				return btn;
			}
			if(btn){
				if(chk_value.length==1){
					if(leixin=="shoushubao"){
//						if(!isNullOrEmpty(keshi)){
							//发放包----------------------------------------------------
							layer.open({
								  type: 2,
								  title: '录入手术包条码',
								  maxmin: true,
								  moveOut:true,
								  shadeClose: true, //点击遮罩关闭层
								  area : ['900px' , '540px'],
								  content: "${ctx}/wupingfafang/wupinshenqing.do?shenqingliushui="+liushui+"&shuliang="+num+"&fafangren="+fafangren,
										  
								  end:function(){
									  window.location.href="${ctx}/shenqing/shenqingList.do";
								  }
							});
					}else{
						//发放一次品---------------------------------------------
						showMask();
						layer.confirm('你确定要发放么？', function(){
							$.ajax({
					             type: "POST",
					             url:"${ctx}/shenqing/wupingfafang.do?shenqingliushui="+chk_value.join()+"&mingxi="+mingxiliushui.join()+"&fafangren="+fafangren,
					             error: function(request) {
					            	 	layer.alert("网络故障，请稍后再试!");
					             },
					             success: function(data) {
					            	 $("#mask").hide();
					            	 if(data.status=="true"){
						 	             layer.alert("发放成功", function(){
						 	             window.location.href="${ctx}/shenqing/shenqingList.do";
						 	             parent.layer.close(index);
					           		     });
					 	             }else{
					 	           	     layer.msg(data.msg);
					 	             }
					             }
					         }); 
						});
					}
				}else{
					layer.msg('请选择并只能选择一批申请流水');
				}
			}
		});
		<%--点击数据前面的选择框后面出结果 --%>
		$(".ycpdata").click(function(){
			/* var ssbnums=${shoushubaoshuliang};
			alert(ssbnums); */
			$(this).addClass("xuanzhong").siblings().removeClass("xuanzhong");
			var leixin= $(this).attr("data");
			var liushuiId=$(this).attr("data2");
			if(leixin==0){
				//手术包
			$.ajax({
	             type: "POST",
	             url:"${ctx}/shenqing/shoushubaoApplyDetailList.do?id="+liushuiId,
	             error: function(request) {
	            	 	layer.alert("网络故障，请稍后再试!");
	             },
	             success: function(data) {
	 	           	if(data.status=="true"){
		 	           	 num = data.shoushubaoshuliang;
		 	           	 appendTable(data);
	 	           	}else{
	 	           		 layer.msg(data.msg);
	 	           	}
	             }
	         }); 
			}else{
				//一次品
				$.ajax({
		             type: "POST",
		             url:"${ctx}/shenqing/yicipinApplyDetailList.do?id="+liushuiId,
		             error: function(request) {
		            	 	layer.alert("网络故障，请稍后再试!");
		             },
		             success: function(data) {
		 	           	if(data.status=="true"){
			 	           	 console.log(data);
			 	           	 var dataList = data.mxList;
			 	           	 var str = '';
			 	           	 $("#detail tbody").empty();
			 	           	 for(var i = 0; i<dataList.length;i++){
			 	           		str += "<tr><td>"+dataList[i].shenqingYicipinMingcheng+"</td><td>"+dataList[i].shenqingYicipinShuliang+"</td><td>"+(dataList[i].fafangZhuangtai==1?"已发":"未发")+"</td></tr>";
			 	           	 }
			 	           	 $("#detail tbody").append(str);
		 	           	}else{
		 	           		 layer.msg(data.msg);
		 	           	}
		             }
		         }); 
			}
		});
		
		<%--$(".packMx").live("dblclick",function(){--%>
			<%--$(this).addClass("xuanzhong").siblings().removeClass("xuanzhong");--%>
			<%--var packId = $(this).attr("data");--%>
			<%--//找儿子的典例--%>
			<%--var name = $(this).find("td:eq(2)").text();--%>
			<%--layer.open({--%>
				  <%--type: 2,--%>
				  <%--title: name+'包明细',--%>
				  <%--maxmin: true,--%>
				  <%--shadeClose: true, //点击遮罩关闭层--%>
				  <%--area : ['800px' , '520px'],--%>
				  <%--content: '${ctx}/wutiaoma/findQixieListPeibao.do?id='+packId+'&name='+name--%>
			<%--});--%>
		<%--});--%>
		
		$("#sureBut").click(function(){
			goPage();
		});
		$("#leftPage").click(function(){
			leftPages();
		});
		$("#rightPage").click(function(){
			rightPages();
		});	
	});

	function getSelRecord(){
		var chk_value =[];
		$('input[name="key"]:checked').each(function(){
			chk_value.push($(this).val()); 
		});
		return chk_value;
	} 
	<%--得到类型--%>
	function getleixin(){
		var check = "yicipin";
		$('input[name="key"]:checked').each(function(){
			var flag=$(this).attr("shuju");
			if(flag==0){
				check="shoushubao";
			}
		});
		return check;
	}  
	<%--得到--%>
	function getshuju(){
		var liushuimingxi =[];
		$('input[name="key"]:checked').each(function(){
			var flag=$(this).attr("shuju");
			liushuimingxi.push($(this).attr("shuju")); 
			
		});
		return liushuimingxi;
	}  
	<%--得到选 中的一行的申请流水--%>
	function getshenqingliushui(){
		var liushui="";
		$('input[name="key"]:checked').each(function(){
			liushui=$(this).val(); 
		});
		return liushui;
	} 
	<%--得到选 中的一行的发放状态--%>
	function getfafangzhuangtai(){
		var fafangzhuangtai="";
		$('input[name="key"]:checked').each(function(){
			fafangzhuangtai=$(this).attr("zhuangtai"); 
		});
		return fafangzhuangtai;
	} 
	
  	function getParameter(name) { 
		var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); 
		var r = window.location.search.substr(1).match(reg); 
		if (r!=null) return unescape(r[2]); return null;
  	}
  	
  	function isNullOrEmpty(strVal){
  		strVal = strVal.replace(/(^\s*)|(\s*$)/g,"");
  		if(strVal == '' || strVal == null || strVal == undefined){
  			return true;
  		}else{
  			return false;
  		}
  	}
  	
  	function ckeckfafangren(){
  	      var ffr=$("#fafangrentiaoma").val();
	  	  $.ajax({
	          type: "POST",
	          url:"${ctx}/shenqing/judgefafangren.do?fafangren="+ffr,
	          error: function(request) {
	         	 	layer.alert("网络故障，请稍后再试!");
	          },
	          success: function(data) {
	           	if(data.status=="false"){
	           		 layer.alert(data.msg);
	           		 window.location.href="${ctx}/shenqing/shenqingList.do";
	           	}
	          }
	      }); 
  	}
  //=================================================================
	function appendTable(dataList){
		 currentPage = 1  ;
         data = dataList.mxList;
         packageId = dataList.packageId;
         creatTab();
	}
  
  	//获得总页数
  	function totalPages(){
  		//如果刚好除尽
  		if(data.length % pageSize == 0){
  			totalPage = data.length / pageSize;
  		}
  		else{
  			totalPage = parseInt(data.length / pageSize) + 1;
  		}
  	}

  	//上一页
  	function leftPages(){
  		if(currentPage != 1){
  			currentPage--;
  			//开始创建
  			creatTab();
  		}
  	}
  	//下一页
  	function rightPages(){
  		if(currentPage != totalPage){
  			currentPage++;
  			//开始创建
  			creatTab();
  		}
  	}
  	//到第几页去
  	function goPage(){
  		currentPage = document.getElementById('goPage').value;
  		//如果输入的页数超出总页数，则到最后一页去！
  		if(currentPage > totalPage){
  			currentPage = totalPage;
  		}
  		if(currentPage < 1){
  			currentPage = 1;
  		}
  		creatTab();
  	}

  	function creatTab(){
  		//清空数据
  		$("#detail tbody").empty();
  		$("#checkalltwo").attr("checked",false);
  		//总页数
  		totalPages();
  		$('#totalNum').text(totalPage);
  		//插入页数
  		$('#goPage').val(currentPage);
  		var str = '';
		$("#detail tbody").empty();
		currentRows  = (totalPage == 1 || currentPage == totalPage) ? data.length : currentPage * pageSize ;
       	for(var i = (currentPage-1) * pageSize; i < currentRows ;i++){
       		str +="<tr class='packMx' data='"+data[i].shenqingWupinbaoMuban+"'><td>"+data[i].shenqingWupinbaoMingcheng+"</td><td>"+data[i].shenqingWupinbaoShuliang+"</td><td>"+(data[i].fafangZhuangtai==1?"已发":"未发")+"</td></tr>";
       	}
        $("#detail tbody").append(str);
  	}
</script>  
</html>